package com.hjy.product.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * ConstraintValidator第一的参数指明效验那一个注解, 第二个表示这个注解下的字段是哪种类型
 */
public class ListValuesConstraintValidator implements ConstraintValidator<ListValues,Integer> {

    private Set<Integer> set = new HashSet<>();
    @Override
    public void initialize(ListValues constraintAnnotation) {
        int[] value = constraintAnnotation.value();
        for (int i : value) {
            set.add(i);
        }
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
